Questions
22 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
22 / 50

How can you use :required and :optional pseudo-classes in forms?

Understanding the :required and :optional Pseudo-Classes in CSS

The :required and :optional pseudo-classes in CSS allow you to style form elements based on whether they are required or optional. These pseudo-classes make it easier to provide visual cues about which inputs must be filled out.

How They Work
  1. 1

    :required – Selects form elements that have the required attribute.

  2. 2

    :optional – Selects form elements that do not have the required attribute.

  3. 3

    These pseudo-classes update dynamically as the user interacts with the form.

Example: Using :required and :optional

In this example, required inputs have a red border and light red background, while optional inputs have a green border and light green background, helping users identify which fields must be filled.

Best Practices
  1. 1

    Use :required and :optional to visually distinguish mandatory fields from optional ones.

  2. 2

    Combine with :focus or :valid/:invalid for dynamic feedback.

  3. 3

    Ensure color contrast and other cues are clear for accessibility.

  4. 4

    Test across different browsers, as styling may vary slightly for form elements.

Difficulty: 3/10
Topics: CSS pseudo-classes, form validation styling, user experience feedback

Scenario Questions

0-2 years experience
  1. 1

    How would you make required form fields show a red border and optional ones a gray dashed border using CSS?

  2. 2

    What happens if you forget to add the required attribute in HTML but try to style it with :required in CSS?

2-5 years experience
  1. 1

    A form works fine in development but the required field indicators don’t show up in production — what could be breaking this, and how would you debug it?

  2. 2

    Your team’s design system uses custom checkboxes, but :required isn’t styling them — how do you fix this without changing the HTML structure?

5-8 years experience
  1. 1

    You’re building a dynamic form builder where fields can toggle between required and optional — how do you ensure the CSS styling remains performant and doesn’t cause layout thrashing?

  2. 2

    How would you design a scalable form component library that uses :required and :optional while supporting theme switching and accessibility compliance across multiple products?

8+ years experience
  1. 1

    We’re migrating a legacy form system that relies on JS-driven validation classes — how do you phase in native CSS :required/:optional without breaking existing behavior or QA test suites?

  2. 2

    How would you advocate for adopting :required and :optional across 20+ products when engineering teams are resistant due to inconsistent browser support in older internal tools?

Follow-up Questions

  • What happens if a field has both required and pattern attributes?
  • How would you handle this if the form is dynamically generated via JavaScript?
  • Would you use these pseudo-classes in a design system? Why or why not?